<?php

namespace app\common\model;

use think\Model;

class Article extends Model
{
    //
    protected $pk='arc_id';
    protected $table='blog_article';
    protected $auto=['admin_id'];
    protected $insert=['sendtime'];
    protected $update=['updatetime'];

    protected function setAdminIdAttr($value)
    {
        return session('admin.admin_id');
    }
    protected function setSendTimeAttr($value)
    {
        return time();
    }
    protected function setUpdateTimeAttr($value)
    {
        return time();
    }

    /*
     *
     * 获取文章首页数据
     */
    public function getAll($is_recycle)
    {
       $data= db('article')->alias('a')->join('__CATE__ c','a.cate_id=c.cate_id')->where('a.is_recycle',$is_recycle)
           ->field('a.arc_id,a.arc_title,a.arc_author,a.sendtime,c.cate_name,a.arc_sort')
           ->order('a.arc_sort desc,a.sendtime desc,a.arc_id asc')
           ->paginate(4);
       return $data;
    }

    /**
     * 添加文章
     * @param $data
     * @return array
     */
    public function store($data)
    {
        //说明添加的时候没有选择标签
        if(!isset($data['tag'])){
            return ['valid'=>0,'msg'=>'请选择标签'];
        }
        //验证
        //添加数据库
        $res=$this->validate(true)->allowField(true)->save($data);
        if($res)
        {
            //文章标签中间表的添加；
            foreach ($data['tag'] as $v) {
                $arcTagData=
                    [
                        'tag_id'=>$v,
                        'arc_id'=>$this->arc_id,
                    ];
                (new ArcTag())->save($arcTagData);
            }
            return ['valid'=>1,'msg'=>'添加文章成功'];
        }else{
            return['valid'=>0,'msg'=>$this->getError()];
        }
    }

    /**
     * 修改文章排序
     */
    public  function changeSort($data)
    {
        $res=$this->validate([
                'arc_sort'=>'require|between:1,9999',
            ],[
                'arc_sort.require'=>'请输入排序',
                'arc_sort.between'=>'排序需要在1-9999之间',
            ])
        ->save($data,['arc_id',$data['arc_id']]);
        if($res){
                return ['valid' => 1, 'msg' => '操作成功'];
        }else{
            return ['valid'=>0,'msg'=>$this->getError()];
        }
    }

    public function edit($data)
    {
        $res=$this->validate(true)->allowField(true)->save($data,['arc_id',$data['arc_id']]);
        if($res)
        {
            //文章标签的删除
            //ArcTag::where('arc_id','=',$data['arc_id'])->delete();
            (new ArcTag())->where('arc_id',$data['arc_id'])->delete();
            //文章标签中间表的添加；
            foreach ($data['tag'] as $v) {
                $arcTagData =
                    [
                        'tag_id' => $v,
                        'arc_id' => $this->arc_id,
                    ];
                (new ArcTag())->save($arcTagData);
            }
            return ['valid'=>1,'msg'=>'操作成功!'];
        }else{
            return ['valid'=>0,'msg'=>'操作失败!'];
        }
    }

    /**
     * 删除文章
     */

    public function del($arc_id)
    {
        if($this::destroy($arc_id))
        {
            //删除成功
            //清除对应文章的标签
            (new ArcTag())->where('arc_id',$arc_id)->delete();
            return ['valid'=>1,'msg'=>'删除成功'];

        }else{
            //删除失败
            return ['valid'=>0,'msg'=>'删除失败'];
        }
    }
}
